package listener;

import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionIdListener;
import javax.servlet.http.HttpSessionListener;


@WebListener
public class OnlineUserListener implements HttpSessionListener, HttpSessionAttributeListener, HttpSessionActivationListener, HttpSessionBindingListener, HttpSessionIdListener, ServletRequestListener, ServletRequestAttributeListener, AsyncListener, ServletContextListener, ServletContextAttributeListener {

 
    public OnlineUserListener() {
       
    }

	
    public void sessionCreated(HttpSessionEvent se)  { 
    	ServletContext context = se.getSession().getServletContext();
        Integer onlineCount = (Integer) context.getAttribute("onlineCount");
        if (onlineCount == null) {
            onlineCount = 1;
        } else {
            onlineCount++;
        }
        context.setAttribute("onlineCount", onlineCount);
        System.out.println("用户上线，当前在线人数：" + onlineCount);
    }

	
    public void attributeRemoved(ServletContextAttributeEvent scae)  { 
         
    }

	
    public void onError(AsyncEvent arg0) throws java.io.IOException { 
         
    }

	
    public void sessionIdChanged(HttpSessionEvent arg0, String arg1)  { 
        
    }

	
    public void attributeAdded(ServletRequestAttributeEvent srae)  { 
         
    }

	
    public void onTimeout(AsyncEvent arg0) throws java.io.IOException { 
         
    }

	
    public void attributeReplaced(HttpSessionBindingEvent se)  { 
         
    }

    public void sessionWillPassivate(HttpSessionEvent se)  { 
        
    }

	
    public void contextInitialized(ServletContextEvent sce)  { 
         
    }

    public void onComplete(AsyncEvent arg0) throws java.io.IOException { 
         
    }

	
    public void attributeAdded(ServletContextAttributeEvent scae)  { 
        
    }

	
    public void requestDestroyed(ServletRequestEvent sre)  { 
         
    }

	
    public void attributeRemoved(ServletRequestAttributeEvent srae)  { 
         
    }

	
    public void onStartAsync(AsyncEvent arg0) throws java.io.IOException { 
         
    }

	
    public void valueBound(HttpSessionBindingEvent event)  { 
        
    }

	
    public void requestInitialized(ServletRequestEvent sre)  { 
         
    }


    public void sessionDestroyed(HttpSessionEvent se)  { 
        
    }

	
    public void sessionDidActivate(HttpSessionEvent se)  { 
    	ServletContext context = se.getSession().getServletContext();
        Integer onlineCount = (Integer) context.getAttribute("onlineCount");
        if (onlineCount == null || onlineCount <= 0) {
            onlineCount = 0;
        } else {
            onlineCount--;
        }
        context.setAttribute("onlineCount", onlineCount);
        System.out.println("用户下线，当前在线人数：" + onlineCount);
    }

	
    public void contextDestroyed(ServletContextEvent sce)  { 
        
    }

	
    public void attributeReplaced(ServletRequestAttributeEvent srae)  { 
         
    }

	
    public void attributeAdded(HttpSessionBindingEvent se)  { 
         
    }

	
    public void attributeRemoved(HttpSessionBindingEvent se)  { 
        
    }

	
    public void attributeReplaced(ServletContextAttributeEvent scae)  { 
         
    }

	
    public void valueUnbound(HttpSessionBindingEvent event)  { 
         
    }
	
}
